<template>
  <div class="login">
    <el-form class="login-form" :model="loginForm" :rules="loginRules" ref="loginForm">
      <h3 class="title">管理系统</h3>

      <el-form-item prop="username">
        <el-input type="text" v-model="loginForm.username" placeholder="账号" ></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input type="password" v-model="loginForm.password" placeholder="密码"></el-input>
      </el-form-item>

      <el-form-item prop="code">
        <el-input type="text" style="width: 63%" v-model="loginForm.code" placeholder="验证码"></el-input>
        <!--   验证码图片     -->
        <div class="login-code">
          <img :src="codeUrl" class="login-code-img" @click="getCode" />
        </div>
      </el-form-item>

      <el-checkbox style="margin: 0 0 25px 0" v-model="loginForm.rememberMe">记住密码</el-checkbox>

      <el-form-item style="width: 100%">
        <el-button type="primary" style="width: 100%" @click.native="handleLogin">
          <span>登 录</span>
        </el-button>
      </el-form-item>
    </el-form>

    <div class="login-footer">
      <span>Copyright 2018</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'

export default {
  name: 'login',
  data () {
    return {
      loginForm: {
        username: '',
        password: '',
        code: '',
        uuid: '',
        rememberMe: false
      },
      codeUrl: '',
      captchaOnOff: true,
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      redirect: undefined
    }
  },
  created () {
    this.getCode()
  },
  watch: {
    $route: {
      handler: function (route) {
        console.log('watch-route', route)
        this.redirect = route.query && route.query.redirect
      },
      // 首次加载也监听
      immediate: true
    }
  },
  methods: {
    getCode () {
      getCodeImg().then(res => {
        console.log('验证码接口', res)
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff
        if (this.captchaOnOff) {
          this.codeUrl = 'data:image/jpeg;base64,' + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$store.dispatch('Login', this.loginForm).then((res) => {
            console.log('login-res', res)
            // this.$router.push('/')
            this.$router.push({ path: this.redirect || '/' })
          }).catch(error => {
            console.error(error)
            if (this.captchaOnOff) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  background-image: url(../assets/images/login-background.jpg);
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;

  .title {
    color: #707070;
    text-align: center;
    margin: 0 auto 30px;
  }

  .login-form {
    box-sizing: border-box;
    width: 400px;
    border-radius: 6px;
    background-color: #fff;
    padding: 25px 25px 5px 25px;
  }

  .login-code {
    width: 33%;
    float: right;
    img{
      cursor: pointer;
    }
    .login-code-img {
      width: 100%;
      height: 100%;
    }
  }

  .login-footer {
    text-align: center;
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0px;
    color: #ffffff;
    font-size: 12px;
    letter-spacing: 1px;
    font-family: Arial;
  }
}
</style>
